package com.ruoyi.mes.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mes.vo.buy.BuyOrderSelectListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.domain.BuyOrder;
import com.ruoyi.mes.service.IBuyOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购订单Controller
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Api(tags = "采购订单")
@RestController
@RequestMapping("/mes/order")
public class BuyOrderController extends BaseController {
    @Autowired
    private IBuyOrderService buyOrderService;

    /**
     * 查询采购订单列表
     */
    @ApiOperation(value = "查询采购订单列表")
    @PreAuthorize("@ss.hasPermi('mes:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuyOrder buyOrder) {
        startPage();
        List<BuyOrder> list = buyOrderService.selectBuyOrderList(buyOrder);
        return getDataTable(list);
    }

    /**
     * 导出采购订单列表
     */
    @ApiOperation(value = "导出采购订单列表")
    @PreAuthorize("@ss.hasPermi('mes:order:export')")
    @Log(title = "采购订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyOrder buyOrder) {
        List<BuyOrder> list = buyOrderService.selectBuyOrderList(buyOrder);
        ExcelUtil<BuyOrder> util = new ExcelUtil<BuyOrder>(BuyOrder.class);
        util.exportExcel(response, list, "采购订单数据");
    }

    /**
     * 获取采购订单详细信息
     */
    @ApiOperation(value = "获取采购订单详细信息")
    @PreAuthorize("@ss.hasPermi('mes:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(buyOrderService.selectBuyOrderById(id));
    }

    /**
     * 新增采购订单
     */
    @ApiOperation(value = "新增采购订单")
    @PreAuthorize("@ss.hasPermi('mes:order:add')")
    @Log(title = "采购订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyOrder buyOrder) {
        return toAjax(buyOrderService.insertBuyOrder(buyOrder));
    }

    /**
     * 修改采购订单
     */
    @ApiOperation(value = "修改采购订单")
    @PreAuthorize("@ss.hasPermi('mes:order:edit')")
    @Log(title = "采购订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyOrder buyOrder) {
        return toAjax(buyOrderService.updateBuyOrder(buyOrder));
    }

    /**
     * 删除采购订单
     */
    @ApiOperation(value = "删除采购订单")
    @PreAuthorize("@ss.hasPermi('mes:order:remove')")
    @Log(title = "采购订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(buyOrderService.deleteBuyOrderByIds(ids));
    }


    //***********************************
    /**
     * 查 - 采购订单分页 - 采购订单编号
     * @param buyOrder
     * @return
     */
    @GetMapping("/buyOrderList")
    public TableDataInfo buyOrderList(BuyOrder buyOrder ){
        startPage();
        List<BuyOrder> list = buyOrderService.buyOrderList(buyOrder);
        return getDataTable(buyOrderService.modifyingPagingData(list));
    }

    /**
     * 删 - 采购订单 - 采购订单id
     * @param id
     * @return
     */
    @DeleteMapping("/deleteBuyOrder")
    public AjaxResult deleteBuyOrder(@RequestParam Long id){
        return buyOrderService.deleteBuyOrder(id);
    }

    /**
     * 查 - 采购订单详情 - 采购订单编码
     * @param buyOrderCode 采购订单编码
     * @return
     */
    @GetMapping("/getDetails")
    public AjaxResult getDetails(@RequestParam String buyOrderCode){
        return success(buyOrderService.selectBuyOrdersByCode(buyOrderCode));
    }

    /**
     * 到货
     * @param id 采购订单id
     * @return
     */
    @PostMapping("/arrival")
    public AjaxResult arrival(@RequestParam Long id){
        return buyOrderService.arrival(id);
    }

    /**
     * 改 - 批量更新采购订单 采购订单list
     * @param buyOrderList
     * @return
     */
    @PutMapping("/batchUpdata")
    public AjaxResult batchUpdata(@RequestBody List<BuyOrder> buyOrderList){
        return success(buyOrderService.batchUpdata(buyOrderList));
    }

    /**
     * 增 - 新增采购订单 采购订单list
     * @param buyOrderList
     * @return
     */
    @PostMapping("/addBuyOrder")
    public AjaxResult addBuyOrder(@RequestBody List<BuyOrder> buyOrderList){
        return toAjax(buyOrderService.addBuyOrder(buyOrderList));
    }


}
